import Crypt from "../../utils/Crypt";
import config from "../../config/config";
import DriverModel from '../../models/Driver'

const model = new DriverModel()

Page({
  data: {
    baseUrl: config.app_base_url,
    username: '',
    password: ''
  },

  onLoad: function (options) {
  },

  bindUsername(e) {
    const value = e.detail.value
    this.setData({ username: value })
  },

  bindPassword(e) {
    const value = e.detail.value
    this.setData({ password: value })
  },

  login() {
    const { username, password } = this.data
    if (!username || !password) {
      return wx.showToast({
        title: '请填写完整',
        icon: 'none'
      })
    }
    model.login({
      username,
      password
    }).then(response => {
      wx.showToast({
        title: '登录成功!',
        icon: 'none'
      })

      let { data } = response
      data = Crypt.parse(data)
      // console.log(data.driver)

      wx.setStorageSync('driver', data.driver)

      setTimeout(() => {
        wx.switchTab({
          url: '/pages/order/index'
        })
      }, 500)

    })
  }



});
